package com.xiaoyu.izf.service;

import com.xiaoyu.izf.config.SysConfig;
import com.xiaoyu.izf.model.CommissionGetLog;
import com.xiaoyu.izf.model.CommissionLog;
import com.xiaoyu.izf.model.UserCardInfo;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.repository.CommissionGetLogDao;
import com.xiaoyu.izf.repository.CommissionLogDao;
import com.xiaoyu.izf.repository.UserCardInfoDao;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Service
public class CommissionGetLogService {

    @Autowired
    private CommissionGetLogDao commissionGetLogDao;
    @Autowired
    private CommissionLogDao commissionLogDao;
    @Autowired
    private UserCardInfoDao userCardInfoDao;

    @Transactional
    public CommissionGetLog add(int uid, int totalAmount){
        CommissionGetLog log = new CommissionGetLog();
        UserCardInfo card = userCardInfoDao.findOneById(uid);
        log.setPayStatus(0);
        log.setUid(uid);
        log.setTotAmount(totalAmount);
        log.setLogTime(new Timestamp(System.currentTimeMillis()));
        log.setBankName(card.getBankName());
        log.setBankAccount(card.getBankAccount());
        log.setBankBranchName(card.getBankBranchName());
        log.setUserName(card.getUserName());
        int handFee = (int)(totalAmount* SysConfig.commissioinGetFee);
        int totAmountTrue = totalAmount-handFee;
        log.setHandFee(handFee);
        log.setTotAmountTrue(totAmountTrue);
        return commissionGetLogDao.save(log);
    }

    @Transactional
    public CommissionGetLog save(CommissionGetLog getLog){
        return commissionGetLogDao.save(getLog);
    }

    public Page<CommissionGetLog> withdrawList(UserInfo userInfo, int pageNumber, int pageSize){
        PageRequest pageRequest = PageRequest.of(pageNumber-1,pageSize, Sort.Direction.DESC,"id");
        Page<CommissionGetLog> page = commissionGetLogDao.findByUid(pageRequest,userInfo.getId());
        return page;
    }

    public Page<CommissionLog> commissionLogs(int uid,int page,int psize){
        PageRequest pageRequest = PageRequest.of(page-1,psize, Sort.Direction.DESC,"id");
        return commissionLogDao.findByUidAndPayStatusAndLogType(pageRequest,uid,1,1);
    }
}
